package com.cn.jinl.service;


import com.cn.jinl.api.IOrderBackInfoService;
import com.cn.jinl.api.IOrderBackStorageService;
import com.cn.jinl.dao.BoxManageMapper;
import com.cn.jinl.dao.OrderBackStorageMapper;
import com.cn.jinl.dao.OrderBackStorageRelateMapper;
import com.cn.jinl.domain.*;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.Constants;
import com.cn.jinl.utils.GenerateUUID;
import com.cn.jinl.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OrderBackStorageServiceImpl implements IOrderBackStorageService {

    @Autowired
    private IOrderBackInfoService orderBackInfoService;

    @Autowired
    private OrderBackStorageMapper orderBackStorageMapper;

    @Autowired
    private OrderBackStorageRelateMapper orderBackStorageRelateMapper;

    @Autowired
    private BoxManageMapper boxManageMapper;

    @Autowired
    private GenerateUUID generateUUID;

    @Override
    public List<OrderBackStorageVo> getOrderBackStorageList(QueryForm queryForm) throws Exception {
        int count = orderBackStorageMapper.selectOrderBackStorageListCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<OrderBackStorageVo> orders = orderBackStorageMapper.selectOrderBackStorageList(queryForm);
        orders.get(0).setCount(count);
        return orders;
    }

    @Override
    public List<OrderBackStorage> getOrderBackStorageById(String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return Lists.newArrayList();
        }
        OrderBackStorageExample example = new OrderBackStorageExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andOrderBackIdEqualTo(id);
        return orderBackStorageMapper.selectByExample(example);
    }

    @Override
    public String addOrderBackStorage(OrderBackStorage orderBackStorage) throws Exception {
        String id = CommonUtil.generateRandomNum("obacks-");
        List<OrderBackStorageRelate> relates = orderBackStorage.getRelates();
        if(CollectionUtils.isEmpty(relates)){
            return "退货箱码不能为空！";
        }
        String orderBackId = orderBackStorage.getOrderBackId();
        if(StringUtils.isEmpty(orderBackId)){
            return "参数【orderBackId】异常！";
        }
        OrderBackInfo exist = orderBackInfoService.getOrderBackInfoById(orderBackId);
        if(!StringUtils.equals("2",exist.getOrderBackStatus())){
            return "不在待入库状态！";
        }
        List<BoxManage> updateBoxes = Lists.newArrayList();
        orderBackStorage.setId(id);
        for(OrderBackStorageRelate relate:relates){
            relate.setBackStorageId(id);
            BoxManage boxManage = new BoxManage();
            boxManage.setBoxCode(relate.getBoxCode());
            boxManage.setCodeStatus("3");
            updateBoxes.add(boxManage);
        }
        int count = orderBackStorageRelateMapper.batchInsertBackStorage(relates);
        if(count < 1){
            return "插入关联表异常！";
        }
        count = boxManageMapper.batchUpdateBoxManageByCode(updateBoxes);
        if(count < 1){
            throw new Exception("更新箱码失败！");
        }

        OrderBackInfo orderBackInfo = new OrderBackInfo();
        orderBackInfo.setId(orderBackId);
        orderBackInfo.setOrderBackStatus("3");
        orderBackInfo.setBackOunt(orderBackStorage.getBackCount());
        String resp = orderBackInfoService.updateOrderBackInfo(orderBackInfo);
        if(!StringUtils.isEmpty(resp)){
            throw new Exception(resp);
        }
        String key = "RK"+generateUUID.getYearMoth();
        int num = generateUUID.getNumberFlag(key);
        String code = key+generateUUID.getThreeNumStr(num);
        orderBackStorage.setBackStorageCode(code);
        orderBackStorage.setBackStorageStatus("1");
        count = orderBackStorageMapper.insertSelective(orderBackStorage);
        if(count < 1){
            throw new Exception("插入主表失败！");
        }
        return "";
    }

    @Override
    public List<OrderBackStorageVo> getAPIOrderBackHasStorageList(QueryForm queryForm) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        Date mothAgo = calendar.getTime();
        String mothStr = simpleDateFormat.format(mothAgo);
        queryForm.setStartTime(mothStr);
        List<OrderBackStorageVo> orderBackStorageVos = orderBackInfoService.getAPIOrderBackHasStorageList(queryForm);
        return orderBackStorageVos;
    }

    @Override
    public List<OrderBackStorageRelate> getAPIOrderBackStorageDetail(String backStorageId) throws Exception {
        if(StringUtils.isEmpty(backStorageId)){
            return Lists.newArrayList();
        }
        OrderBackStorageRelateExample example = new OrderBackStorageRelateExample();
        example.createCriteria().andBackStorageIdEqualTo(backStorageId);
        return orderBackStorageRelateMapper.selectByExample(example);
    }
}
